#!/usr/bin/env python3
#
#  Copyright 2016 The TMM/AD Authors. All rights reserved.
#  Use of this source code is governed by a BSD-style license that can be
#  found in the license.txt file.
#

import threading as _threading

_TERMINAL_IO_LOCK = _threading.Lock()


def _terminal_io_lock_acquire():
    """Acquire the terminal I/O lock."""

    _TERMINAL_IO_LOCK.acquire()


def _terminal_io_lock_release():
    """Release the terminal I/O lock."""

    _TERMINAL_IO_LOCK.release()


def terminal_print(text, thread_safe=True):
    """Print text on terminal.

    :param text: The text.
    :param thread_safe: Whether the program should be in thread-safe mode.
    """

    #  Lock.
    if thread_safe:
        _terminal_io_lock_acquire()

    #  Do printing.
    print(text)

    #  Unlock.
    if thread_safe:
        _terminal_io_lock_release()
